﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Simulation.Framework
{
    public class AudioManager : ManagerBehavior
    {
        private Dictionary<string, List<AudioExtensions>> audiosPool = new Dictionary<string, List<AudioExtensions>>();
        private AudioExtensions bgmAudioExtensions;
        private string playStrBGM = "";
        private string playStrEffet = "";
        #region override

        public override void Init()
        {
            audiosPool = new Dictionary<string, List<AudioExtensions>>();
        }

        public override void Clear()
        {
           audiosPool?.Clear();
            audiosPool = null;
          
        }
        public void ClearAuio()
        {
           // if (bgmAudioExtensions != null) bgmAudioExtensions = null;
        }
        public override void Restar() { }

        #endregion

        public void PlayBgm(string name,UnityAction onComplete = null, EResPathType resPathType = EResPathType.resources)
        {
            UnityAction<AudioClip> addAudioExtensions = audio =>
            {
                if (audio == null)
                {
                    onComplete?.Invoke();
                    return;
                }

                if(bgmAudioExtensions==null) bgmAudioExtensions = new AudioExtensions(audio);
                bgmAudioExtensions.PlayAudio(audio);
                //AudioExtensions audioExtensions = new AudioExtensions(audio);
                bgmAudioExtensions.type = AudioExtensions.AudioType.BGM;
                bgmAudioExtensions.onComplete = onComplete;
                bgmAudioExtensions.audioSource.volume = 0.5f;
                //List<AudioExtensions> audios = null;
                //if (audiosPool.ContainsKey(name))
                //    audios = audiosPool[name];
                //else
                //    audios = new List<AudioExtensions>();
                //audios.Add(bgmAudioExtensions);
                //audiosPool[name] = audios;
                playStrBGM=name;
            };

            if (resPathType == EResPathType.resources)
            {
                AudioClip clip = SingleBehavior<ResManagerByResources>.getInstance().Load<AudioClip>(name);
                addAudioExtensions(clip);
            }
            else
            {
                SingleBehavior<ResManagerByNet>.getInstance().LoadAudio(name, clip => {
                    addAudioExtensions(clip);
                });
            }
        }

        public void PlayEffect(string name, UnityAction onComplete = null, EResPathType resPathType = EResPathType.resources)
        {
            UnityAction<AudioClip> addAudioExtensions = audio =>
            {
                if (audio == null)
                {
                    onComplete?.Invoke();
                    return;
                }

                AudioExtensions audioExtensions = new AudioExtensions(audio);
                audioExtensions.type = AudioExtensions.AudioType.EFFECT;
                audioExtensions.onComplete = () => {
                    audiosPool[name].Remove(audioExtensions);
                    onComplete?.Invoke();
                };

                List<AudioExtensions> audios = null;
                if (audiosPool.ContainsKey(name))
                    audios = audiosPool[name];
                else
                    audios = new List<AudioExtensions>();
                audios.Add(audioExtensions);
                audiosPool[name] = audios;
                playStrEffet = name;
            };

            if (resPathType == EResPathType.resources)
            {
                AudioClip clip = SingleBehavior<ResManagerByResources>.getInstance().Load<AudioClip>(name);
                addAudioExtensions(clip);
            }
            else
            {
                SingleBehavior<ResManagerByNet>.getInstance().LoadAudio(name, clip => {
                    addAudioExtensions(clip);
                });
            }
        }

       

        public void PauseAudio(string fullName)
        {
            if (audiosPool.ContainsKey(fullName))
            {
                List<AudioExtensions> audioExtensions = audiosPool[fullName];
                for (int i = 0; i < audioExtensions.Count; i++)
                    audioExtensions[i].Pause();
            }
        }

        public void UnPauseAudio(string fullName)
        {
            if (audiosPool.ContainsKey(fullName))
            {
                List<AudioExtensions> audioExtensions = audiosPool[fullName];
                for (int i = 0; i < audioExtensions.Count; i++)
                    audioExtensions[i].UnPause();
            }
        }

        public void StopAudio(string fullName)
        {
            if (fullName == null)
                return;
            if (audiosPool.ContainsKey(fullName))
            {
                List<AudioExtensions> audioExtensions = audiosPool[fullName];
                for (int i = 0; i < audioExtensions.Count; i++)
                    audioExtensions[i].Stop();
                audiosPool[fullName].Clear();
                audiosPool.Remove(fullName);
            }
        }
        public void StopAudio()
        {
            //bgmAudioExtensions = null;
            string fullName = playStrEffet;
            if (fullName == null)
                return;
            if (audiosPool.ContainsKey(fullName))
            {
                List<AudioExtensions> audioExtensions = audiosPool[fullName];
                for (int i = 0; i < audioExtensions.Count; i++)
                    audioExtensions[i].Stop();
                audiosPool[fullName].Clear();
                audiosPool.Remove(fullName);
            }

        }
        public List<AudioExtensions> GetAudioExtensions(string key) {
            if (audiosPool.ContainsKey(key))
                return audiosPool[key];
            return null;
        }
    }
}